import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

import org.hibernate.EntityMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.collection.PersistentSet;
import org.apache.log4j.PropertyConfigurator;

public class TestClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		Configuration config = new Configuration();
		config.configure();
		
		SessionFactory sessionFactory = config.buildSessionFactory();
		Session session = sessionFactory.openSession();
		session = session.getSession(EntityMode.MAP);
		
		Transaction tx = null;
		HashMap user = null;
		try {
			 
			 /* user = (HashMap) session.get("cfhibernate.model.user.user", new Integer(17));
			 
			 PersistentSet addresses = (PersistentSet) user.get("addresses");
			 Iterator addressIT = addresses.iterator();
			 HashMap address = null;
			 while (addressIT.hasNext()) {
				 address = (HashMap) addressIT.next();
			 }
			 System.out.println("Address1 was: " + address.get("address1"));
			 address.put("address1", "1234 Jordan Way");
			 System.out.println("Address1 now is: " + address.get("address1"));
			 */
			
			user = new HashMap();
			HashSet addresses = new HashSet();
			user.put("id", new Integer(0));
			user.put("firstName", "Java");
			user.put("lastName", "Tester");
			user.put("email", "kw@aol.com");
			HashMap address = new HashMap();
			address.put("id", new Integer(0));
			address.put("address1", "18 Walter Payton Way");
			address.put("city", "Chicago");
			address.put("state", "IL");
			address.put("zip", "343434");
			address.put("type", "cfhibernate.model.user.address.address");
			addresses.add(address);
			user.put("addresses", addresses);
			user.put("type", "cfhibernate.model.user.user");
			System.out.println("User: " + user);
			
			tx = session.beginTransaction();
			session.saveOrUpdate(user);
			tx.commit();
		} catch (Exception e) {
			throw e;
		} finally {
			session.close();
		}
		
		System.out.println("Done.");
		
		sessionFactory.close();
		
	}

}
